import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/pages/mall_app/account_safe/state.dart';
import 'package:yp/services/api.dart';

class AccountSafeController extends GetxController {
  final state = AccountSafeState();
  @override
  void onInit() {
    super.onInit();
    var _mobile = SpUtil.getString('number');
    if (_mobile.length > 4) {
      var sufixLength = (_mobile.length - 4) ~/ 2;
      var prefixLength = _mobile.length - 4 - sufixLength;
      state.photo = _mobile.substring(0, prefixLength) +
          '****' +
          _mobile.substring(_mobile.length - sufixLength, _mobile.length);
    } else {
      state.photo = _mobile;
    }
    queryUserInfo();
  }

  void queryUserInfo() {
    var userNumId = Get.arguments != null && Get.arguments['userNum'] != null
        ? Get.arguments['userNum']
        : SpUtil.getInt('empeNumId');
    var params = {'usr_num_id': userNumId, 'card_type': 1};
    Api.queryUserCard(params).listen((event) {
      if (event['code'] == 0) {
        state.freeOpenSign = event['free_open_sign'] == 1;
        state.passwordFlag = event['password_flag'] == 1;
      }
      update();
    });
  }

  void setNoPwdPay() {
    EasyLoading.instance.userInteractions = false;
    EasyLoading.show();
    var userNumId = Get.arguments != null && Get.arguments['userNum'] != null
        ? Get.arguments['userNum']
        : SpUtil.getInt('empeNumId');
    var params = {
      'usr_num_id': userNumId,
      'card_type': 1,
      'free_open_sign': state.freeOpenSign ? 0 : 1, //开通免密支付标识 0 不开通 1开通
      'free_amount': 200
    };
    Api.noPwdPay(params).listen((event) {
      if (event['code'] == 0) {
        state.freeOpenSign = !state.freeOpenSign;
      }
      EasyLoading.dismiss();
      update();
    });
  }
}
